package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.app.Advertising;
import com.ruoyi.system.domain.app.DeptWxInfo;
import com.ruoyi.system.domain.app.Feedback;
import com.ruoyi.system.domain.app.UserManual;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.app.AdvertisingMapper;
import com.ruoyi.system.mapper.app.DeptWxInfoMapper;
import com.ruoyi.system.mapper.app.FeedbackMapper;
import com.ruoyi.system.mapper.app.UserManualMapper;
import com.ruoyi.system.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    AdvertisingMapper advertisingMapper;
    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    UserManualMapper userManualMapper;
    @Autowired
    DeptWxInfoMapper deptWxInfoMapper;
    @Autowired
    SysDeptMapper deptMapper;



    @Override
    public void addAd(Advertising advertising) {
        advertising.setAppId(getAppId());
        advertising.setCreateTime(new Date());
        advertisingMapper.insertSelective(advertising);
    }

    @Override
    public void delAd(Long id) {
        advertisingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Advertising> adList(Advertising advertising) {
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            return advertisingMapper.adList(null);
        }
        return advertisingMapper.adList(getAppId());
    }

    @Override
    public List<Feedback> feedbackList(String userName, String phone, String deviceNum) {
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            return feedbackMapper.feedbackList(getAppId(),userName,phone,deviceNum);
        }
        return feedbackMapper.feedbackList(getAppId(),userName,phone,deviceNum);
    }

    @Override
    public List<Feedback> issueFeedbackList(String userName, String phone, String deviceNum) {
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            return feedbackMapper.issueFeedbackList(getAppId(),userName,phone,deviceNum);
        }
        return feedbackMapper.issueFeedbackList(getAppId(),userName,phone,deviceNum);
    }

    @Override
    public void feedbackSet(Long id) {
        Feedback feedback = new Feedback();
        feedback.setId(id);
        feedback.setFeedbackState(1);
        feedbackMapper.updateByPrimaryKeySelective(feedback);
    }

    @Override
    public UserManual getProto() {
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            return userManualMapper.selectByAppid(null);
        }
         return userManualMapper.selectByAppid(getAppId());


    }

    @Override
    public void setProto(UserManual userManual) {

        userManualMapper.updateByPrimaryKeySelective(userManual);
    }

    @Override
    public List<UserManual> getHelp() {
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            return userManualMapper.getHelp(null);
        }
        return userManualMapper.getHelp(getAppId());

    }

    @Override
    public void setHelp(List<UserManual> userManuals) {
        userManualMapper.delAll(2,getAppId());
        for (UserManual userManual : userManuals) {
            userManualMapper.insertSelective(userManual);
        }
    }
    
    private String getAppId(){
        SysDept sysDept = deptMapper.selectDeptById(SecurityUtils.getDeptId());
        String[] longs = sysDept.getAncestors().split(",");
        if (longs.length > 2) {
            return deptWxInfoMapper.selectByPrimaryKey(Long.valueOf(longs[2])).getAppId();
        }
        return deptWxInfoMapper.selectByPrimaryKey(sysDept.getDeptId()).getAppId();
    }
}
